import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
 
public class test1
    implements Runnable {
 
    static final int APP_WIDTH = 640;
    static final int APP_HEIGHT = 480;
 
    private GraphicsEnvironment mGraphicsEnv = null;
    private GraphicsDevice mGraphicsDev = null;
    private GraphicsConfiguration mGraphicsConf = null;
    private BufferStrategy mBufferStrategy = null;
 
    private JFrame mFrame = null;
 
    private Graphics2D mGraphics = null;
 
    private long mStart = 0;
    private long mEnd = 0;
    private long mDT;
    private double mSeconds;
 
    private long mSetFPS = (1000/60) * 1000000;
 
    private int mBoxX = 200;
    private int mBoxY = 200;
    private int mBoxDX = 1;
    private int mBoxDY = 1;
 
    test1() {
        mGraphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
        mGraphicsDev = mGraphicsEnv.getDefaultScreenDevice();
        mGraphicsConf = mGraphicsDev.getDefaultConfiguration();
 
        mFrame = new JFrame(mGraphicsConf);
        mFrame.setIgnoreRepaint(true);
        mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mFrame.setLocation(100, 100);
        mFrame.setSize(APP_WIDTH, APP_HEIGHT);
        mFrame.setFocusTraversalKeysEnabled(false);
        mFrame.setResizable(false);
        mFrame.setVisible(true);
 
        mFrame.createBufferStrategy(2);
        mBufferStrategy = mFrame.getBufferStrategy();
 
        Thread thread = new Thread(this);
        thread.start();
    }
 
    public void run() {
        while(true) {
            mStart = System.nanoTime();
 
            mBoxX += mBoxDX;
            mBoxY += mBoxDY;
 
            if(mBoxX > APP_WIDTH - 50)
                mBoxDX = -1;
            if(mBoxX < 0)
                mBoxDX = 1;
            if(mBoxY > APP_HEIGHT - 50)
                mBoxDY = -1;
            if(mBoxY < 0)
                mBoxDY = 1;
 
            mGraphics = (Graphics2D)mBufferStrategy.getDrawGraphics();
            mGraphics.clearRect(0, 0, APP_WIDTH, APP_HEIGHT);
            mGraphics.setColor(Color.RED);
            mGraphics.fillRect(mBoxX, mBoxY, 50, 50);
 
            mGraphics.dispose();
            mBufferStrategy.show();
 
            mEnd = System.nanoTime();
            mDT = mEnd - mStart;
 
            while(mDT < mSetFPS) {
                Thread.yield();
                mEnd = System.nanoTime();
                mDT = mEnd - mStart;
            }   
        }
    }
 
    public static void main(String[] args) {
        new test1();
    }
}